import { getAction, deleteAction, putAction, postAction, httpAction } from '@/api/manage'
import Vue from 'vue'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

//角色管理
const addRole = (params) => postAction('/sys/role/add', params)
const editRole = (params) => putAction('/sys/role/edit', params)
const queryall = (params) => getAction('/sys/role/queryall', params)

//用户管理
const addUser = (params) => postAction('/sys/user/add', params)
const editUser = (params) => putAction('/sys/user/edit', params)
const queryUserRole = (params) => getAction('/sys/user/query_user_role', params)
const getUserList = (params) => getAction('/sys/user/list', params)
const frozenBatch = (params) => putAction('/sys/user/frozen_batch', params)

//验证用户是否存在
const checkOnlyUser = (params) => getAction('/sys/user/checkOnlyUser', params)

//改变密码
const changePassword = (params) => putAction('/sys/user/change_password', params)

//权限管理
const addPermission = (params) => postAction('/sys/permission/add', params)
const editPermission = (params) => putAction('/sys/permission/edit', params)
const getPermissionList = (params) => getAction('/sys/permission/tree', params)
const getSystemMenuList = (params) => getAction('/sys/permission/getSystemMenuList', params)
const getSystemSubmenu = (params) => getAction('/sys/permission/getSystemSubmenu', params)
const getSystemSubmenuBatch = (params) => getAction('/sys/permission/getSystemSubmenuBatch', params)
const queryTreeList = (params) => getAction('/sys/permission/query_tree', params)
const queryTreeListForRole = (params) => getAction('/sys/permission/query_tree', params)
const queryListAsync = (params) => getAction('/sys/permission/queryListAsync', params)
const queryRolePermission = (params) => getAction('/sys/permission/query_role_permission', params)
const saveRolePermission = (params) => postAction('/sys/permission/save_role_permission', params)
const queryPermissionsByUser = () => getAction('/sys/permission/get_user_permission_by_token')
const loadAllRoleIds = (params) => getAction('/sys/permission/loadAllRoleIds', params)
const getPermissionRuleList = (params) => getAction('/sys/permission/getPermRuleListByPermId', params)
const queryPermissionRule = (params) => getAction('/sys/permission/queryPermissionRule', params)

// 区域管理
const queryAreaTreeList = (params) => getAction('/sys/region/tree', params)
const addArea = (params) => postAction('/sys/region/add', params)
const editArea = (params) => postAction('/sys/region/edit', params)
const deleteByAreaId = (params) => deleteAction('/sys/region/delete', params)

//数据字典
const addDict = (params) => postAction('/sys/dict/add', params)
const editDict = (params) => putAction('/sys/dict/edit', params)
const addDictItem = (params) => postAction('/sys/dict_item/add', params)
const editDictItem = (params) => putAction('/sys/dict_item/edit', params)

//字典标签专用（通过code获取字典数组）
export const ajaxGetDictItems = (params) => getAction(`/sys/dict/find_enable_dict_items_by_code`, params)
//从缓存中获取字典配置
function getDictItemsFromCache (dictCode) {
  if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    let dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]
    //console.log("-----------getDictItemsFromCache----------dictCode="+dictCode+"---- dictItems=",dictItems)
    return dictItems
  }
}

// 根据部门主键查询用户信息
const queryUserByDepId = (params) => getAction('/sys/user/queryUserByDepId', params)

// 重复校验
const duplicateCheck = (params) => getAction('/sys/duplicate/check', params)
// 加载分类字典
const checkRuleByCode = (params) => getAction('/sys/checkRule/checkByCode', params)


//获取验证码
const randomImage = (params) => getAction('/sys/random/image')
// 检查验证码
const checkCaptcha = (params) => postAction('/sys/random/check_captcha', params)

//树形结构
const getOneFlagTreeList = (params) => getAction('/sys/unit/user_tree', params) // 用户所属区域的机关机构


//富文本上传图片/视频
const uploadUeditorImg = (params) => postAction('/sys/upload/upload_file', params)

// 上传图片
const uploadFile = (params) => postAction('/sys/upload/upload_file', params)
//获取图片路径前缀
const findEnableDictItemsbyCode = (params) => getAction('/sys/dict/find_enable_dict_items_by_code', params)

// 刷新token
const handelRefreshToken = (params) => postAction('/sys/operate/refresh_token', params)

// 单位树
const unitTreeList = (params) => getAction('/sys/unit/tree', params)
const unitAdd = (params) => postAction('/sys/unit/add', params)
const unitEdit = (params) => postAction('/sys/unit/edit', params)
const unitDel = (params) => deleteAction('/sys/unit/delete', params)



export {
  addRole,
  editRole,
  addUser,
  editUser,
  queryUserRole,
  getUserList,
  queryall,
  frozenBatch,
  checkOnlyUser,
  changePassword,
  getPermissionList,
  addPermission,
  editPermission,
  queryTreeList,
  queryListAsync,
  queryRolePermission,
  saveRolePermission,
  queryPermissionsByUser,
  loadAllRoleIds,
  getPermissionRuleList,
  queryPermissionRule,
  addDict,
  editDict,
  addDictItem,
  editDictItem,
  queryUserByDepId,
  duplicateCheck,
  queryTreeListForRole,
  getSystemMenuList,
  getSystemSubmenu,
  getSystemSubmenuBatch,
  checkRuleByCode,
  getDictItemsFromCache,
  randomImage,
  checkCaptcha,
  addArea,
  editArea,
  queryAreaTreeList,
  deleteByAreaId,
  // queryIdTree,
  // queryParentName,
  getOneFlagTreeList,
  uploadUeditorImg,
  uploadFile,
  findEnableDictItemsbyCode,
  handelRefreshToken,

  unitTreeList,
  unitAdd,
  unitEdit,
  unitDel
}